package com.yc.forum.dao;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.*;

public class RedisDao {
    private JedisPool jp=new JedisPool();
//    通过帖子主键id的字符串查询帖子的likesum  commentsum collectsum
    public List<Map<String,Object>> selectPost(String ids) {
        //一次查询10个
        List<Map<String, Object>> list = new ArrayList<>();
        try (Jedis jedis = jp.getResource()) {
            System.out.println(ids + "===传入后台post_id");
            String[] id = ids.split("-");
            for (int i = 0; i < id.length; i++) {
                Map<String, Object> map = new HashMap<>();
                String post_id = "post_" + id[i];
                map.put("post_id", post_id);
                map.put("likesum", jedis.hget(post_id, "likesum")==null?0:jedis.hget(post_id, "likesum"));
                map.put("commentsum", jedis.hget(post_id, "commentsum")==null?0:jedis.hget(post_id, "commentsum"));
                map.put("collectsum", jedis.hget(post_id, "collectsum")==null?0:jedis.hget(post_id, "collectsum"));
                list.add(map);
            }
        }
        return list;
    }
    Jedis jedis;
    {
//        try {
            jedis = jp.getResource();
//        }
    }
/* -------------------------------------分割线----------------------------------------  */
    public static void main(String[] args) {
        RedisDao redisDao=new RedisDao();
        int a=redisDao.getreward("12345");
        System.out.println(a);
    }
    //修改用户积分
    public void updateReward(String account,int reward){
        System.out.println("updateReward begin");
        jedis.set("integral_"+account,""+reward);
        System.out.println("updateReward end");
    }
    //获取用户的积分
    public int getreward(String account){
        return jedis.get("integral_"+account)==null?0:Integer.parseInt(jedis.get("integral_"+account));
    }
    /*添加用户浏览记录*/
    public int addBrowsePostId(String postId,String account){
        return (int)jedis.sadd("browse_"+account,postId);
    }
    /*-----------------用户收藏帖子----------------*/
    //判断当前登录用户是否收藏了该篇文章
    public boolean accountIsCollectionThePost(String postId,String account){
        return jedis.sismember("collection_"+account,postId);
    }
    //将该帖子id添加到用户收藏集合中
    public int addCollectionPostId(String postId,String account){
        return (int)jedis.sadd("collection_"+account,postId);
    }
    //帖子收藏数量改变
    public int addPostCollectionSumById(String postid,int collectsum){
        return (int)jedis.hset("post_"+postid,"collectsum",""+collectsum);
    }
    /*------------------------用户点赞帖子------------------*/
    //为当前登录用户存入点赞的帖子id
    public int addSupportPostId(String postId,String account){
        return (int)jedis.sadd("like_"+account,postId);
    }
    /* 判断当前登录是账号是否点赞了该条帖子*/
    public boolean accountIsInArray(String postId,String account){
        return jedis.sismember("post_supp_account_"+postId,account);
    }
    /* 将给帖子点赞的人的账号添加到数组中*/
    public int addPostSupportAccount(String postId,String account){
        return (int)jedis.sadd("post_supp_account_"+postId,account);
    }
    /* 帖子点赞数量改变*/
    public int addPostSupportSumById(String postid,int likesum){
        return (int)jedis.hset("post_"+postid,"likesum",""+likesum);
    }

    //获取该评论点赞、评论、收藏数量
    public Map<String,String> queryReplySomeSumById(String replyid){
        Map<String,String> map=jedis.hgetAll("reply_"+replyid);
        //null=>0
        map.put("likesum",map.get("likesum")==null?"0":map.get("likesum"));
        map.put("commentsum",map.get("commentsum")==null?"0":map.get("commentsum"));
        map.put("collectsum",map.get("collectsum")==null?"0":map.get("collectsum"));
        System.out.println("------------------------------------------------------");
        System.out.println(map.toString());
        return map;
    }
    //获取该帖子点赞、评论、收藏数量
    public Map<String,String> queryPostSomeSumById(String postid){
        Map<String,String> map=jedis.hgetAll("post_"+postid);
        map.put("likesum",map.get("likesum")!=null?map.get("likesum"):"0");
        map.put("commentsum",map.get("commentsum")!=null?map.get("commentsum"):"0");
        map.put("collectsum",map.get("collectsum")!=null?map.get("collectsum"):"0");
        map.put("readsum",map.get("readsum")!=null?map.get("readsum"):"0");
        return map;
    }

    //获取该用户收藏帖子的id集合
    public List<String> queryPostIdListByAccount(String account){
        Set<String> set=jedis.smembers("collection_"+account);
        List<String> list1 = new ArrayList<String>(set);
        return list1;
    }

    //获取用户收藏记录的长度
    public int getUserCollectionPostLengthByAccount(String account){
        return (int)jedis.scard("collection_"+account);
    }
    //获取该用户点赞帖子的id集合
    public List<String> queryLikesumPostIdListByAccount(String account){
        Set<String> set=jedis.smembers("like_"+account);
        List<String> list1 = new ArrayList<String>(set);
        return list1;
    }
    //获取用户点赞记录的长度
    public int getUserLikesumPostLengthByAccount(String account){
        return (int)jedis.scard("like_"+account);
    }

    //获取该用户浏览帖子的id集合
    public List<String> queryBrowsePostIdListByAccount(String account){
        Set<String> set=jedis.smembers("browse_"+account);
        List<String> list1 = new ArrayList<String>(set);
        return list1;
    }
    //获取用户浏览记录的长度
    public int getUserBrowsePostLengthByAccount(String account){return (int)jedis.scard("browse_"+account);}
    //添加或修改帖子的点赞数量、评论数量、收藏数量
    public void updatePostSomeSum(String postId,Map<String,String> someSum){
        String ok= jedis.hmset("post_"+postId,someSum);
    }


    //添加用户收藏的帖子的id
    public void updateUserCollection(String account,String postid){
        long ok=jedis.sadd("collection_"+account,postid);
    }
    //添加用户浏览记录的帖子的id
    public void updateUserBrowse(String account,String postid){
        long ok=jedis.sadd("browse_"+account,postid);
    }
    //添加用户滇藏的帖子的id
    public void updateUserLike(String account,String postid){
        long ok=jedis.sadd("like_"+account,postid);
    }
    //添加给该帖子点赞的账号
    public void updatePostSuppAccount(String postid,String account){
        long ok=jedis.sadd("post_supp_account_"+postid,account);
    }




    //添加给该评论点赞的账号
    public void updateReplySuppAccount(String replyid,String account){
        long ok=jedis.sadd("reply_supp_account_"+replyid,account);
    }
    //添加或修改回复的点赞数量、评论数量、收藏数量
    public void updateReplySomeSum(String replyId,Map<String,String> someSum){
        String ok= jedis.hmset("reply_"+replyId,someSum);
    }
    //获取给该评论点赞的所有账号
    public List<String> queryReplySuppAccount(String replyId){
        Set<String> set=jedis.smembers("reply_supp_account_"+replyId);
        List<String> newList=new ArrayList<>(set);
        return newList;
    }
}
